package org.dandsoft.blackbox.knowledge;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

import java.io.Serializable;
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * User: dvilyuzhanin
 * Date: 13.01.2009 16:03:50
 */
public class EntityInfo extends AbstractInfo<EntityInfo> implements Serializable {
    private static final long serialVersionUID = 7485265805943738531L;

    public final static String ENTITY_GROPUP_NAME = "entities";
    private static final String ENTITY_ELEMENT = "entity";
    public final static String TYPE = ENTITY_ELEMENT;
    public final static DataFormat FORMAT = DataFormat.Xml;
    private static final String VERSION_ELEMENT = "version";
    protected final static String VERSION_PARAMETER = VERSION_ELEMENT;
    private static final String NAME_ELEMENT = "name";
    protected final static String NAME_PARAMETER = NAME_ELEMENT;
    private static final String PACKAGE_ELEMENT = "package";
    protected final static String PACKAGE_NAME_PARAMETER = PACKAGE_ELEMENT;
    private static final String DESCRIPTION_ELEMENT = "description";
    protected final static String DESCRIPTION_PARAMETER = DESCRIPTION_ELEMENT;

    private Version version;
    private String description;

    public EntityInfo() {
        super(ENTITY_GROPUP_NAME, TYPE, FORMAT);
    }

    public void store(OutputStream out) throws IOException {
        Document document = new Document();
        Element root = new Element(ENTITY_ELEMENT);
        document.setRootElement(root);

        if (version != null) {
            XMLUtil.createElement(root, VERSION_ELEMENT, version.format());
        }
        XMLUtil.createElement(root, NAME_ELEMENT, getName().getName());
        if (getName().getPackageName() != null) {
            XMLUtil.createElement(root, PACKAGE_ELEMENT, getName().getPackageName());
        }

        XMLUtil.createElement(root, DESCRIPTION_ELEMENT, description);

        XMLOutputter outputer = new XMLOutputter();
        outputer.output(document, out);
    }

    public void load(InputStream in) throws IOException {
        SAXBuilder builder = new SAXBuilder();
        try {
            Document document = builder.build(in);
            Element root = document.getRootElement();

            XMLUtil.assertElement(root, ENTITY_ELEMENT);

            Element versionElement = root.getChild(VERSION_ELEMENT);
            if (versionElement != null) {
                setVersion(Version.parse(versionElement.getTextNormalize()));
            }

            String packageName = null;
            Element packageElement = root.getChild(PACKAGE_ELEMENT);
            if (packageElement != null) {
                packageName = packageElement.getTextNormalize();
            }

            Element nameElement = root.getChild(NAME_ELEMENT);
            if (nameElement == null) {
                throw new Exception("Element entity.name not found.");
            }

            Element descriptionElement = root.getChild(DESCRIPTION_ELEMENT);
            if (descriptionElement != null) {
                setDescription(descriptionElement.getTextNormalize());
            }

            setName(new InfoName(nameElement.getTextNormalize(), packageName));

        } catch (Exception e) {
            throw new IOException(e);
        }
    }


    public Version getVersion() {
        return version;
    }

    public void setVersion(Version version) {
        this.version = version;
    }


    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }


    public String toString() {
        return "EntityInfo{" +
               "version=" + version +
               "name=" + super.getName() +
               ", description='" + description + '\'' +
               '}';
    }
}
